#include "LoadListFiles.h"

namespace FPTK{

loadlistfiles::loadlistfiles(char *filename,int _nb,bool _useref,std::ostream *_out)
{
out=_out;
FILE *ptfile;
char** list_temp;
useref=_useref;
extension=NULL;
ptfile=fopen(filename/*argv[1]*/,"r");
if(_nb==0)
   {
   _nb=10000;
   }
if(ptfile) //try to read the file: if impossible, exit of the program
   {
   //*(_out)<<"Open "<<filename<<std::endl;
   list_temp = new char*[_nb];
   for(int p=0;p<_nb;p++)
      list_temp[p]=new char[150];
   int q=0;
   while(q<_nb && fgets((list_temp[q]), 150, ptfile)!=NULL)//read the list of image to transform
      {
      if(list_temp[q][strlen(list_temp[q])-1]=='\n') list_temp[q][strlen(list_temp[q])-1]='\0';
      q++;
      }
   fclose(ptfile);
   for(int i=0;i<q;i++)
      {
      //if(!strcmp(list_temp[i],"") || !(strstr(list_temp[i],".mha") || strstr(list_temp[i],".gipl")))
        if(!strcmp(list_temp[i],"") || !(CheckExtension(list_temp[i],"mha") || CheckExtension(list_temp[i],"gipl") || CheckExtension(list_temp[i],"meta")))
         {
         for(int j=i;j<q-1;j++) strcpy(list_temp[j],list_temp[j+1]);
         q--;
         i--;
         }
      }
   nb=q;
   if(nb>0)
      {
      ref=new char[strlen(list_temp[0])+1];
      strcpy(ref,list_temp[0]);
      }
   }
else
   {
   *(_out)<<"Cannot open "<<filename<<std::endl;
   list=NULL;
   nb=0;
   }
if(useref)
   {
   list=new char*[nb];
   for(int i=0;i<nb;i++)
      {
      list[i]=new char[150];
      strcpy(list[i],list_temp[i]);
      }
   }
if(!useref)
   {
   list=new char*[nb-1];
   for(int i=0;i<nb-1;i++)
      {
      list[i]=new char[150];
      strcpy(list[i],list_temp[i+1]);
      }
   nb--;
   }
current=0;
}



loadlistfiles::~loadlistfiles()
{
for(int i=0;i<nb;i++)
   {
   delete []list[i];
   }
delete []list;
}



int loadlistfiles::GetNbFiles()
{
return nb;
}

char* loadlistfiles::GetCurrentFile()
{
if(current<nb)
   {
   return list[current];
   }
else
   {
   return NULL;
   }
}

int loadlistfiles::SetFileToGet(int _current)
{
if(_current<nb)
   {
   current=_current;
   return 0;
   }
return -1;
}

int loadlistfiles::Increment()
{
return ++*this;
}


int loadlistfiles::operator++()
{
current++;
return current;
}

int loadlistfiles::operator++(int notused)
{
return ++*this;
}

int loadlistfiles::Decrement()
{
return --*this;
}

int loadlistfiles::operator--(int notused)
{
return --*this;
}


int loadlistfiles::operator--()
{
if(current>0)
   {
   current--;
   }
return current;
}




char* loadlistfiles::GetRefFile()
{
return ref;
}


int loadlistfiles::SetNbFile(int _nb)
{
if(_nb<nb)
   {
   nb=_nb;
   return 0;
   }
return -1;
}


bool loadlistfiles::IsFinished()
{
if(current>=nb)
   {
   return 1;
   }
else
   {
   return 0;
   }
}



void loadlistfiles::PrintListFiles()
{
for(int e=0;e<nb;e++)
   {
   (*out)<<list[e]<<std::endl;//print the list of image to transform
   }
}



char* loadlistfiles::GetExtensionCurrentFile()
{
 delete []extension;
 char* pospt;
 pospt=strchr(list[current],'.');
 if(pospt!=NULL)
 {
     extension=new char[strlen(pospt)];
    pospt++;
     strcpy(extension,pospt);
 }
 return extension;
}


bool loadlistfiles::CheckExtension(char* name,char* ext)
{
    char* pospt;
    pospt=strchr(name,'.');
    if(pospt!=NULL)
    {
        pospt++;
        return !strcmp(pospt,ext);
    }
    return false;
}

} //end namespace FPTK